package com.guofen.controller.sjq;

import com.guofen.pojo.SjqWaterProductionElectricityConsumption;
import com.guofen.service.sjq.SjqWaterProductionElectricityConsumptionService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/sjq/sjqWaterProductionElectricityConsumption")
@Controller
@Api(tags = "SjqWaterProductionElectricityConsumptionController", description = "水处理生产现场记录表（用电)")
public class SjqWaterProductionElectricityConsumptionController {
    @Autowired
    private SjqWaterProductionElectricityConsumptionService sjqWaterProductionElectricityConsumptionService;

    @ApiOperation("添加记录")
    @RequestMapping(value = "/addForm", method = RequestMethod.POST)
    @ResponseBody
    public Result addForm(@ApiParam(name = "meter_reading_time", value = "抄表时间", required = true) @RequestParam String meter_reading_time,
                          @ApiParam(name = "w_meter_reading", value = "水处理表读数", required = true) @RequestParam String w_meter_reading,
                          @ApiParam(name = "c_meter_reading", value = "炭化表读数", required = true) @RequestParam String c_meter_reading,
                          @ApiParam(name = "reading_person", value = "抄表人", required = true) @RequestParam String reading_person,
                          @ApiParam(name = "remarks", value = "备注", required = false) @RequestParam String remarks){
        Double reading_num = Double.parseDouble(w_meter_reading);
        Double reading_num1 = Double.parseDouble(c_meter_reading);
        if (sjqWaterProductionElectricityConsumptionService.addForm(meter_reading_time, reading_num, reading_num1, reading_person,  remarks))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("查询记录")
    @RequestMapping(value = "/findForm", method = RequestMethod.GET)
    @ResponseBody
    public Result findForm(){
        if (sjqWaterProductionElectricityConsumptionService.findForm() != null)
            return ResultGenerator.genSuccessResult(sjqWaterProductionElectricityConsumptionService.findForm());
        else
            return ResultGenerator.genFailResult("暂无记录");
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(sjqWaterProductionElectricityConsumptionService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name = "meter_reading_time", value = "抄表时间", required = true) @RequestParam String meter_reading_time,
                         @ApiParam(name = "w_meter_reading", value = "水处理表读数", required = true) @RequestParam String w_meter_reading,
                         @ApiParam(name = "c_meter_reading", value = "炭化表读数", required = true) @RequestParam String c_meter_reading,
                         @ApiParam(name = "reading_person", value = "抄表人", required = true) @RequestParam String reading_person,
                         @ApiParam(name = "remarks", value = "备注", required = false) @RequestParam String remarks) {
        Double reading_num = Double.parseDouble(w_meter_reading);
        Double reading_num1 = Double.parseDouble(c_meter_reading);
        SjqWaterProductionElectricityConsumption record = new SjqWaterProductionElectricityConsumption();
        record.setId(id);
        record.setMeterReadingTime(meter_reading_time);
        record.setwMeterReading(reading_num);
        record.setcMeterReading(reading_num1);
        record.setReadingPerson(reading_person);
        record.setRemarks(remarks);
        return ResultGenerator.genSuccessResult(sjqWaterProductionElectricityConsumptionService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(sjqWaterProductionElectricityConsumptionService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(sjqWaterProductionElectricityConsumptionService.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
